<template>
  <div id="globalHeader">
    <a-row align="center" :wrap="false">
      <a-col flex="100px">
        <div>100px</div>
      </a-col>
      <a-col flex="auto">
        <a-menu
          mode="horizontal"
          :selected-keys="selectedKey"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img src="@/assets/logo.jpg" alt="" class="logo" />
              <div class="title">OJ</div>
            </div>
          </a-menu-item>
          <a-menu-item :key="item.path" v-for="item in visibleRoutes"
            >{{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="100px">
        <div>{{ store.state.user?.loginUser?.userName ?? "未登录" }}</div>
      </a-col>
    </a-row>
  </div>
</template>
<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRoute, useRouter } from "vue-router";
import { computed, onMounted, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

const router = useRouter();
let selectedKey = ref(["/"]);
const route = useRoute();

// 路由跳转后，，更新路由之后
router.afterEach((to, from, failure) => {
  selectedKey.value = [to.path];
});

const doMenuClick = (key: string) => {
  router.push(key);
};

const store = useStore();
console.log(store.state.user.loginUser);

const visibleRoutes = computed(() => {
  // 动态获取改变的loginUser
  const loginUser = store.state.user.loginUser;
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    return checkAccess(loginUser, item.meta?.access);
  });
});

// setTimeout(() => {
//   // store.
//   store.dispatch("user/getLoginUser", {
//     userName: "cc",
//     userRole: ACCESS_ENUM.ADMIN,
//   });
// }, 3000);

// console.log(visibleRoutes, "routes");
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title-bar .logo {
  height: 48px;
}

.title-bar .title {
  color: #444;
  margin-left: 16px;
}
</style>
